package structure;

import java.util.ArrayList;

import algorithm.miscellaneous;

public class item {

	private String leftside;
	private String rightside;
	private ArrayList<String> lookahead;
	private String afterDot;
	private boolean isComplete;
	private int indexAfterDot;
	private int indexDot;
	private int index;
	private int indexBigProduction;
	private boolean isCreator;
	
	
	public item(){

		this.leftside= null;
		this.rightside= null;
		this.lookahead= null;
		this.afterDot= null;
		this.isComplete= false;
		this.indexAfterDot= -1;
		this.indexDot= -1;
		this.index= -1;
		this.indexBigProduction= -1;
		this.isCreator= false;
	}
	
	public item(String left, String right, ArrayList<String> look){
		
		this.leftside= left;
		this.rightside= right;
		if(look == null)
			this.lookahead= new ArrayList<String>();
		else
			this.lookahead= look;
		
		this.indexDot= right.indexOf(".");

		if(this.getIndexDot() == right.length()-1)
			this.isComplete= true;
		else
			this.isComplete= false;
		
		if(this.isComplete()){
			this.afterDot= "";
			this.indexAfterDot= -1;
		}
		else{
			int index= this.getIndexDot();
			this.indexAfterDot= index+1;
			this.afterDot= this.rightside.substring(index+1, index+2);
		}
		
		this.index= -1;
		this.indexBigProduction= -1;
		this.isCreator= false;
	}
	
	public item clone() throws CloneNotSupportedException{
		
		item i= new item();
		i.leftside= new String(this.leftside);
		i.rightside= new String(this.rightside);
		i.afterDot= new String(this.afterDot);
		i.isComplete= this.isComplete;
		i.lookahead= (ArrayList<String>) this.lookahead.clone();
		i.indexAfterDot= this.indexAfterDot;
		i.indexDot= this.indexDot;
		i.isCreator= this.isCreator;
		return i;
	}
	
	//set
	public void setLookahead(ArrayList<String> look){
		
		this.lookahead.clear();
		miscellaneous.addAllOnce(look, this.lookahead);
		//this.lookahead.addAll(look);
	}
	
	public void setIndex(int i){
		
		this.index= i;
	}
	
	public void setIndexBigProduction(int i){
		
		this.indexBigProduction= i;
	}
	
	public void setIndexAfterDot(int i){
		
		this.indexAfterDot= i;
	}
	
	public void setIndexDot(int i){
		
		this.indexDot= i;
	}
	
	public void setCreator(){
		
		this.isCreator= true;
	}
	
	public boolean isCreator(){
		
		return this.isCreator;
	}
	
	public int getIndexBigProduction(){
		
		return this.indexBigProduction;
	}
	
	public int getIndex(){
		
		return this.index;
	}

	public ArrayList<String> getLookahead(){
		
		return this.lookahead;
	}

	//get
	public int getIndexDot(){
		
		return this.indexDot;
	}
	
	public int getIndexAfterDot(){
		
		return this.indexAfterDot;
	}
	
	public String getRightside(){
		
		return this.rightside;
	}
	
	public String getLeftside(){
		
		return this.leftside;
	}
	
	public String getAfterDot(){
		
		return this.afterDot;
	}
	
	public String getRightsideClean(){
		
		/*char[] tmpC= this.rightside.toCharArray();
		char[] toAdd= new char[tmpC.length];
		for(int k=0; k<tmpC.length; k++){
			
			char c= tmpC[k];
			if(c != '.')
				toAdd[k]= c;
		}*/
		String toReturn= "";
		String beforeDot= this.rightside.substring(0, this.indexDot);
		String afterDot= this.rightside.substring(this.indexDot+1);
		if(this.rightside.equals(".")){
			toReturn= "e";
		}
		else if(beforeDot.equals("")){
			toReturn= afterDot;
		}
		else if(afterDot.equals("")){
			toReturn= beforeDot;
		}
		else if(beforeDot.equals("") && afterDot.equals("")){
			toReturn= "e";
		}
		else{
			toReturn= beforeDot + afterDot;
		}
		
		return toReturn;
	}

	public boolean isComplete(){
		
		return this.isComplete;
	}
	
	public boolean isFirst(){
		
		if(this.getIndexDot()==0){
			return true;
		}
		
		return false;
	}
	
	public boolean sameLookahead(ArrayList<String> lookahead){
		
		if(this.lookahead.size() != lookahead.size()) 
			return false;
		
			/*for(int i=0; i<this.lookahead.size(); i++){
	
				if(!this.lookahead.get(i).equals(lookahead.get(i))){
					
					return false;
				}
			}*/
			return this.lookahead.containsAll(lookahead);
		
		
	}
	
	public boolean sameAs(item item){
		
		//stesso leftside
		//System.out.println("leftside= "+ item.getLeftside());
		if(this.leftside.equals(item.getLeftside())){
			//stesso rightside
			//System.out.println("rigthside= "+ item.getRightside());
			if(this.rightside.equals(item.getRightside())){
				//stesso lookahead
				if(this.sameLookahead(item.getLookahead())){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public boolean sameAsWL(item item){
		
		//stesso leftside
		if(this.leftside.equals(item.getLeftside())){
			//stesso rightside
			if(this.rightside.equals(item.getRightside())){
				
					return true;
		
			}
		}
		
		return false;
	}
	
	public boolean hasLookahead(){
		
		if(this.lookahead.isEmpty() || this.lookahead == null){
			return false;
		}
		
		return true;
	}
	
	public void printItAll(){

		//System.out.println("(" + this.leftside + "->" + this.rightside + ", " + this.lookahead + ")");
		System.out.print("(" + this.leftside + "->" + this.rightside + ", " + this.lookahead + ")");
		if(this.isCreator) System.out.print("Creator");
	}
}